﻿/*
*
*   Jianquan's javascript lib
*   Copyright (c) Jianquan
*   Email:lijianquan07@gmail.com
*   QQ:55643287
*   Version 1.0.0
*/


(function () {
    if (!window['LJQ']) {
        window['LJQ'] = {}
    }
    var _pos_global = { "top": 0, "left": 0 };
    function drag(target, handle) {
        _getMousePostion = function (event) {
            event = event || window.event;
            var _top = 0, _left = 0;
            if (event.pageX && event.pageY) {
                _left = event.pageX;
                _top = event.pageY;
            }
            else if (event.clientX && event.clientY) {
                _left = event.clientX + document.body.scrollLeft - document.body.clientLeft;
                _top = event.clientY + document.body.scrollTop - document.body.clientY;
            }
            else {
                return;
            }
            return { x: _left, y: _top };
        };
        var _offsetLeft = 0, _offsetTop = 0;
        _startDrag = function (event) {
            _doDrag();
        };
        _doDrag = function () {
            $(handle).mousemove(function (event) {
                var pos = _getMousePostion(event);
                $(target).css({
                    "position": "absolute",
                    "top": (parseInt(pos.y) - _offsetTop) + "px",
                    "left": (parseInt(pos.x) - _offsetLeft) + "px"
                });
            });
        };
        _stopDrag = function () {
            $(handle).unbind("mousemove", null);
        };
        $(handle).mousedown(function (event) {
            _targetDoc = document.getElementById(target.replace("#", ""));
            _pos_global = { "top": 0, "left": 0 };
            while (_targetDoc.offsetParent) {
                _pos_global.top += _targetDoc.offsetTop;
                _pos_global.left += _targetDoc.offsetLeft;
                _targetDoc = _targetDoc.offsetParent;
            }
            _pos_global.top += _targetDoc.offsetTop;
            _pos_global.left += _targetDoc.offsetLeft;
            _offsetLeft = _getMousePostion(event).x - _pos_global.left;
            _offsetTop = _getMousePostion(event).y - _pos_global.top;
            _startDrag();
        });
        $(handle).mouseup(function () {
            _stopDrag();
        });

    }
    window['LJQ']['drag'] = drag;
    function toFix(target) {
        if (parseFloat(target) == target) {
            return Math.round(target * 100) / 100;
        }
        else {
            return target;
        }
    }
    window['LJQ']['toFix'] = toFix;
})();